
import { Component, OnInit, ViewChild } from '@angular/core';
import 'rxjs/add/operator/map';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import 'rxjs/add/operator/toPromise';
declare const $: any;
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';

@Component({
    selector: 'app-report-ddd',
    templateUrl: './ddd.component.html',
    styleUrls: ['./ddd.component.scss']
})

export class DddComponent implements OnInit {
    title = '设备故障停机明细';
    public fwList: any[];
    constructor(private commServices: CommService) {
    }
    ngOnInit() {
        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD HH:mm');
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        this.equipmentSelected = this.equipemntItems;

        const queryModel: QueryModel = new QueryModel();
        this.commServices
            .getList(queryModel, ReportUrls.TipFaultType)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.typeItems.push({ type: '全部' });
                    model.data.forEach(e => {
                        this.typeItems.push(e);
                    });
                    this.typeSelected = this.typeItems[0];
                } else if (model.resultCode === 300) {
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }
    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public shiftItems: any[];
    public typeItems = new Array();
    public equipemntItems: TVResource[] = [];
    public subAareSelected: any;
    public shiftSelected: any;
    public typeSelected: any;
    public equipmentSelected: TVResource[];

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;
    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }

    onSubAreaSelectedChange(): void {
        debugger
        this.equipmentSelected = [];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAareSelected.subArea);
        this.equipmentSelected = this.equipemntItems;
        debugger
    }
    /**
     * 获取表格数据
     */
    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.resource);
        queryModel.shift = this.shiftSelected.shiftId;
        queryModel.tag = this.typeSelected.type;

        this.commServices
            .getList(queryModel, ReportUrls.TipFault)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    model.data.forEach(item =>{
                        if(queryModel.querySubAreaIds[0].slice(0,2) === 'GF'){
                            var sub: string = '';
                            sub = item.subArea.replace(/(^\s*)|(\s*$)/g, "");
                            switch(sub){
                                case 'GFAL1': sub += '_装配主线';  break;
                                case 'GFBU1': sub += '_测试线';  break;
                                case 'GFCL1': sub += '_CASE 机加工线';  break;
                                case 'GFCR1': sub += '_行星齿轮组分装线';  break;
                                case 'GFCS1': sub += '_中间支撑架分装线';  break;
                                case 'GFDH1': sub += '_飞轮盘减震器壳体分装线';  break;
                                case 'GFHL1': sub += '_HOUSING 机加工线';  break;
                                case 'GFTC1': sub += '_壳体分装线';  break;
                                case 'GFTD1': sub += '_中间轴&差速器分装线';  break;
                            }
                            item.subArea = sub;
                        }
                    });
                    this.fwList = model.data;
                } else if (model.resultCode === 300) {
                    alert('没有查询到数据...');
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }

    onExporting(e) {
        e.fileName = e.fileName + new Date().toISOString().replace(/[\-\:\.]/g, '').split('T')[0];
    }
}
